#include <winsock2.h>
#include "Dialog/ChannelInfoPanel.h"
#include "../../common/protocol.h"
#include <netlib/packet/packetpool.h>
#include "Network/NetworkManager.h"

ChannelInfoPanel::ChannelInfoPanel() : m_channelInfo( NULL )
{
}

ChannelInfoPanel::~ChannelInfoPanel()
{
}

bool ChannelInfoPanel::create( interfacelib::Window::LoadControl *loadControl, int index )
{
	char name[ MAX_CONTROL_NAME ];
	wsprintf( name, "%s%d", SCRIPT_CHANNEL_INFO, index );

	interfacelib::Window::LoadControl::ControlProperty *property = loadControl->getControlProperty( name );
	if ( property )
		Panel::create( property->resourceName, property->pos.x, property->pos.y, property->attribute );

	loadControl->loadChild( SCRIPT_CHANNEL_INFO_CONTROL, this );

	m_editChannelName = ( interfacelib::Window::EditBox * )loadControl->getControl( SCRIPT_CHANNEL_INFO_NAME, interfacelib::Window::Window::WindowType::EDITBOX );
	
	return ( true );
}

void ChannelInfoPanel::setChannelInfo( ChannelInfo *channelInfo )
{
	m_channelInfo = channelInfo;

	if ( channelInfo )
		m_editChannelName->setString( channelInfo->name );
	else 
		m_editChannelName->clear();
}

bool ChannelInfoPanel::sendEnterChannelReq( char *password )
{
	if ( !m_channelInfo ) return ( false );
	SYNC_GET_PACKET( syncGetPacket, enterChannelPacket );

	enterChannelPacket->ID( protocol::community::EnterChannelReq );
	*enterChannelPacket << m_channelInfo->number;
	if ( password ) *enterChannelPacket << password;

	NetworkManager::getInstance()->send( enterChannelPacket );

	return ( true );
}

void ChannelInfoPanel::onLButtonUp( UINT flags, POINT point )
{
	sendEnterChannelReq();
}